package com.hs.ph.Dao.Impl;

import com.hs.ph.Dao.CommunityDao;
import com.hs.ph.Entity.Community;
import com.hs.ph.Entity.Shop;
import com.hs.ph.Utils.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

//9.31完成
public class CommunityDaoImpl implements CommunityDao {

    @Override
    public int updateShopCommunityId(String[] ids, int communityId) {
        int num =0;
        if(ids.length!=0){
            for(String s : ids){
                int shopid = Integer.parseInt(s);
                String sql = "update shop set communityId=? where shopId=?";
                Object[] obj = {shopid,communityId};
                JDBCDruidUtil.update(sql, obj);
                num++;
            }
        }else {
            return 0;
        }
        if (num!=ids.length){
            return 0;
        }
        return num;
    }


    /**
     * 查看关联门店的总total
     * @param map
     * @return
     */
    @Override
    public int selectGuanLianCount(Map<String, String> map) {

        List list1 = new ArrayList();
        String sql = "select count(1) from shop where communityId=? ";
        String communityid = map.get("communityId");
        int communityId=0;
        if(communityid!=null&&!"".equals(communityid)){
            communityId = Integer.parseInt(communityid);
        }
        list1.add(communityId);
        String shopName = map.get("shopName");
        if (shopName!=null&&!"".equals(shopName)){
            sql+=" and shopName like ? ";
            list1.add("%"+shopName+"%");
        }

        Object[] obj = list1.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        int total = 0;
        while (true){
            try {
                if (!res.next()) break;
                total=res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        JDBCDruidUtil.close();
        return total;
    }



    /**
     * 查看关联门店
     * @param map
     * @return
     */
    @Override
    public List<Shop> selectGuanLian(Map<String, String> map) {
        List<Shop> list = new ArrayList<>();

        List list1 = new ArrayList();
        String sql = "select * from shop where communityId=? ";
        String communityid = map.get("communityId");
        int communityId=0;
        if(communityid!=null&&!"".equals(communityid)){
            communityId = Integer.parseInt(communityid);
        }
        list1.add(communityId);
        String shopName = map.get("shopName");
        if (shopName!=null&&!"".equals(shopName)){
            sql+=" and shopName like ? ";
            list1.add("%"+shopName+"%");
        }

        String pageNum = map.get("pageNum");
        if (pageNum!=null&&!"".equals(pageNum)){
            sql+=" limit ?,? ";
            Integer pageNum1 = Integer.parseInt(pageNum);
            String pageSize=map.get("pageSize");
            Integer pageSize1 = Integer.parseInt(pageSize);
            list1.add((pageNum1-1)*pageSize1);
            list1.add(pageSize1);
        }

        Object[] obj = list1.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while(true){
            try {
                if (!res.next()) break;
                list.add(new Shop(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(3),
                        res.getString(4),
                        res.getString(5),
                        res.getString(6),
                        res.getString(7),
                        res.getString(8),
                        res.getString(9),
                        res.getString(10),
                        res.getString(11),
                        res.getInt(12),
                        res.getDate(13),
                        res.getInt(14),
                        res.getString(15),
                        res.getString(16),
                        res.getString(17),
                        res.getInt(18),
                        res.getString(19),
                        res.getString(20),
                        res.getInt(21),
                        res.getInt(22),
                        res.getString(23),
                        res.getString(24),
                        res.getInt(25)
                ));
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        JDBCDruidUtil.close();
        return list;
    }



    @Override
    public int insert(Community community) {
        String sql="insert into community values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object [] obj = new Object[]{
        community.getCommunityName(),
        community.getCommunityProvince(),
        community.getCommunityCity(),
        community.getCommunityDistrict(),
        community.getCommunityAddress(),
        community.getCommunityLatitude(),
        community.getCommunityLongitude(),
        new Date(),
        community.getCommunityShopCount(),
        community.getCommunityAreaCode(),
        community.getCommunityCoordinates(),
        community.getCommunityStatus(),
        community.getCommunityUserCount(),
        community.getDistance()
        };
        int num = JDBCDruidUtil.update(sql, obj);
        return num;
    }

    @Override
    public int delete(int communityId) {
        String sql="delete from community where communityId=?";
        Object [] obj = new Object[]{communityId};
        int num = JDBCDruidUtil.update(sql, obj);
        return num;
    }

    @Override
    public int update(Community community) {
        String sql="update community set communityName=?,communityProvince=?,communityCity=?,communityDistrict=?,communityAddress=?,communityLatitude=?,communityLongitude=?,createTime=? where communityId=?";
        Object [] obj = new Object[]{
                community.getCommunityName(),
                community.getCommunityProvince(),
                community.getCommunityCity(),
                community.getCommunityDistrict(),
                community.getCommunityAddress(),
                community.getCommunityLatitude(),
                community.getCommunityLongitude(),
                new Date(),
                community.getCommunityId()
        };
        int num = JDBCDruidUtil.update(sql, obj);
        return num;
    }

    @Override
    public List<Community> selectAll(Map<String, String> map) {
        List<Community> communities = new ArrayList<>();
        String sql="select * from community where 1=1 ";
        List list=new ArrayList();
        String communityName = map.get("communityName");
        String startTime = map.get("startTime");
        String pageNum = map.get("pageNum");

        if (communityName!=null&&!"".equals(communityName)){
            sql+=" and communityName like ? ";
            list.add("%"+communityName+"%");
        }

        if (startTime!=null&&!"".equals(startTime)){
            sql+=" and createTime between ? and ? ";
            list.add(startTime);
            list.add(map.get("endTime"));
        }


        if (pageNum!=null&&!"".equals(pageNum)){
            sql+=" limit ?,? ";
            Integer pageNum1 = Integer.parseInt(pageNum);
            String pageSize=map.get("pageSize");
            Integer pageSize1 = Integer.parseInt(pageSize);
            list.add((pageNum1-1)*pageSize1);
            list.add(pageSize1);
        }

        Object[] obj = list.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true){
            try {
                if (!res.next()) break;
                communities.add(new Community(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(3),
                        res.getString(4),
                        res.getString(5),
                        res.getString(6),
                        res.getString(7),
                        res.getString(8),
                        res.getDate(9),
                        res.getInt(10),
                        res.getInt(11),
                        res.getString(12),
                        res.getInt(13),
                        res.getInt(14),
                        res.getString(15)
                ));
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }

        JDBCDruidUtil.close();
        return communities;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        String sql="select count(1) from community where 1=1 ";
        List list=new ArrayList();

        String communityName = map.get("communityName");
        String startTime = map.get("startTime");

        if (communityName!=null&&!"".equals(communityName)){
            sql+=" and communityName like ? ";
            list.add("%"+communityName+"%");
        }

        if (startTime!=null&&!"".equals(startTime)){
            sql+=" and createTime between ? and ? ";
            list.add(startTime);
            list.add(map.get("endTime"));
        }
        Object[] obj = list.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        int count = 0;
        while (true){
            try {
                if (!res.next()) break;
                count=res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }

        JDBCDruidUtil.close();
        return count;
    }
}
